package view;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JDialog;
import javax.swing.KeyStroke;

import java.awt.Color;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import domain.User;

import persistence.ClienteBbdd;
import persistence.IPersistence;
import persistence.UserNotFoundException;


import java.awt.Insets;
import javax.swing.JTextPane;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.sql.SQLException;

public class JDialogId extends JDialog {

	private static final long serialVersionUID = 1L;
	private JPanel jPanel = null;
	private JButton jButtonIdentificacion = null;
	private JTextField jTextFieldLogin = null;
	private JPasswordField jPasswordField = null;
	private AppKini ownerApp = null;
	private IPersistence bbdd = null;  //  @jve:decl-index=0:
	private JTextPane info = null;
	
	public JDialogId(AppKini owner1) {
		super(owner1.getMainJFrame());
		ownerApp = owner1;
		initialize();
	}

	private void initialize() {
		setTitle("Login");
		setSize(300, 200);
		setContentPane(getJPanel());
		setAlwaysOnTop(true);
		setModal(true);
		getJPanel().getRootPane().setDefaultButton(getJButtonIdentificacion());

		addWindowListener(new java.awt.event.WindowAdapter() {   
			public void windowClosing(java.awt.event.WindowEvent e) {    
				ownerApp.getMainJFrame().dispose();
			}
		});
	}
	
	private IPersistence getBbdd()
	{
		if (bbdd == null)
		{
			getInfo().setText("Conectando...");
			try {
				bbdd = ClienteBbdd.getInstance();
				bbdd.connect();
			} catch (ClassNotFoundException e) {
				getInfo().setText("No se puede cargar el driver: "+ e.getMessage());
				close();
				return null;
			} catch (SQLException e) {
				getInfo().setText("Error al obtener la conexion: "+ e.getMessage());
				close();
				return null;
			}
		}
		return bbdd;
	}

	private void close() {
		getInfo().setText("Cerrando");
		if (getBbdd()!=null)
			getBbdd().disconnect();
		getOwner().dispose();
		dispose();
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.fill = GridBagConstraints.BOTH;
			gridBagConstraints11.gridy = 0;
			gridBagConstraints11.weightx = 1.0;
			gridBagConstraints11.weighty = 1.0;
			gridBagConstraints11.ipady = 20;
			gridBagConstraints11.anchor = GridBagConstraints.CENTER;
			gridBagConstraints11.gridheight = 1;
			gridBagConstraints11.gridx = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints2.gridy = 3;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.ipadx = 150;
			gridBagConstraints2.insets = new Insets(0, 0, 10, 0);
			gridBagConstraints2.ipady = 10;
			gridBagConstraints2.gridx = 1;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints1.gridy = 2;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.ipadx = 150;
			gridBagConstraints1.gridwidth = 1;
			gridBagConstraints1.insets = new Insets(0, 0, 10, 0);
			gridBagConstraints1.anchor = GridBagConstraints.CENTER;
			gridBagConstraints1.ipady = 10;
			gridBagConstraints1.gridx = 1;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 1;
			gridBagConstraints.ipadx = 0;
			gridBagConstraints.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints.insets = new Insets(0, 0, 10, 0);
			gridBagConstraints.gridy = 5;
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.setBackground(SystemColor.control);
			jPanel.add(getJButtonIdentificacion(), gridBagConstraints);
			jPanel.add(getJTextFieldLogin(), gridBagConstraints1);
			jPanel.add(getJPasswordField(), gridBagConstraints2);
			jPanel.add(getInfo(), gridBagConstraints11);
			}
		return jPanel;
	}
	

	private void login() {
		try {
			getInfo().setText("Contrastando credenciales...");
			if (getBbdd()!=null){
				User usr = getBbdd().getUser(getJTextFieldLogin().getText(), getJPasswordField().getPassword());
				setAlwaysOnTop(false);
				ownerApp.setUser(usr);
				dispose();
			}
		} catch (UserNotFoundException e1) {
			getInfo().setText(e1.getMessage());
		} catch (SQLException e2) {
			getInfo().setText(e2.getMessage());
		}
	}

	private JButton getJButtonIdentificacion() {
		if (jButtonIdentificacion == null) {
			jButtonIdentificacion = new JButton();
			jButtonIdentificacion.setText("Login");
			jButtonIdentificacion.setFocusable(true);
			jButtonIdentificacion.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					login();
				}
			});
		}
		return jButtonIdentificacion;
	}

	/**
	 * This method initializes jTextFieldLogin	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldLogin() {
		if (jTextFieldLogin == null) {
			jTextFieldLogin = new JTextField();
			jTextFieldLogin.setBackground(Color.white);
		}
		return jTextFieldLogin;
	}

	/**
	 * This method initializes jPasswordField	
	 * 	
	 * @return javax.swing.JPasswordField	
	 */
	private JPasswordField getJPasswordField() {
		if (jPasswordField == null) {
			jPasswordField = new JPasswordField();
		}
		return jPasswordField;
	}

	/**
	 * This method initializes info	
	 * 	
	 * @return javax.swing.JTextPane	
	 */
	private JTextPane getInfo() {
		if (info == null) {
			info = new JTextPane();
			info.setEditable(false);
			info.setBackground(SystemColor.control);
			info.setEnabled(true);
		}
		return info;
	}

}
